package com.bayair.doctor.biz.helper;

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.huawei.HuaWeiRegister;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.alibaba.sdk.android.push.register.MiPushRegister;
import com.bayair.doctor.BuildConfig;
import com.bayair.doctor.biz.im_helper.ConfigHelper;
import com.bayair.doctor.biz.im_helper.IMConfig;
import com.bayair.doctor.utils.log.KLog;
import com.blankj.utilcode.util.Utils;
import com.example.library.XUI;
import com.hjq.image.ImageLoader;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.imsdk.session.SessionWrapper;
import com.tencent.qcloud.tim.uikit.TUIKit;

public class SDKHelper {

    private static final String TAG = "SDKHelper";

    private static volatile SDKHelper singleton;

    private SDKHelper() {
    }

    public static SDKHelper getInstance() {
        if (singleton == null) {
            synchronized (SDKHelper.class) {
                if (singleton == null) {
                    singleton = new SDKHelper();
                }
            }
        }
        return singleton;
    }

    private Context mContext;

    public void init(Application application) {
        mContext = application.getApplicationContext();

        Utils.init(application);
        XUI.init(application);

        CrashReport.initCrashReport(application, "3a38abd9d5", true);

        SingleUtils.init();

        ImageLoader.init(application);

        //InitBusiness.start(application, TIMLogLevel.DEBUG);

        initIM();

        initCloudChannel(application);

        KLog.init(BuildConfig.LOG_DEBUG, "KLog");//日志
    }

    //腾讯IM
    private void initIM() {
        //判断是否是在主线程
        if (SessionWrapper.isMainProcess(mContext)) {
            TUIKit.init(mContext, IMConfig.SDKAPPID, new ConfigHelper().getConfigs());
        }
    }


    /**
     * 初始化阿里云推送通道
     *
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        this.createNotificationChannel();
        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        //拦截到intentservice中处理推送来的消息
        //pushService.setPushIntentService(PushMessageIntentService.class);
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                String deviceId = pushService.getDeviceId();
                Log.d(TAG, "推送初始化成功:" + deviceId);

            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.d(TAG, "推送初始化错误 -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });

        Log.d(TAG, "==================================MiPushRegister================================start==");
        MiPushRegister.register(applicationContext, "2882303761518269321", "5751826973321"); // 初始化小米辅助推送
        HuaWeiRegister.register((Application) applicationContext); // 接入华为辅助推送
        Log.d(TAG, "==================================MiPushRegister=================================end=");
        //MeizuRegister.register(applicationContext, "MEIZU_ID", "MEIZU_KEY");
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            // 通知渠道的id
            String id = "1";
            // 用户可以看到的通知渠道的名字.
            CharSequence name = "notification channel";
            // 用户可以看到的通知渠道的描述
            String description = "notification description";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
            // 配置通知渠道的属性
            mChannel.setDescription(description);
            // 设置通知出现时的闪灯（如果 android 设备支持的话）
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.GREEN);
            // 设置通知出现时的震动（如果 android 设备支持的话）
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            //最后在notificationmanager中创建该通知渠道
            mNotificationManager.createNotificationChannel(mChannel);
        }
    }
}